from threading import Thread
from queue import Queue


class MyProducer(Thread):
    def __init__(self, name: str, count: int, queue: Queue):
        super().__init__()

        self.setName(name)
        self.count = count
        self.queue = queue

    def run(self) -> None:
        for n in range(self.count):
            msg = f"{self.getName()} - {n}"
            self.queue.put(msg, block=True)  # 如果满了，等待


class MyConsumer(Thread):
    def __init__(self, name: str, queue: Queue):
        super().__init__()

        self.setName(name)
        self.setDaemon(True)  # 守护线程，主线程结束，立刻结束
        self.queue = queue

    def run(self) -> None:
        # 一直运行
        while True:
            msg = self.queue.get(block=True)  # 没数据，等待
            print(f"{self.getName()} - {msg}\n", end="")


queue = Queue(3)
threads = list()

threads.append(MyProducer("PA", 10, queue))
threads.append(MyProducer("PB", 10, queue))
threads.append(MyProducer("PC", 10, queue))

threads.append(MyConsumer("CA", queue))
threads.append(MyConsumer("CB", queue))


for t in threads:
    t.start()
